using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CaseData;
//using System.Collections.Generic;

namespace CaseManager {

    public enum CaseAction {
        New,
        Update
    }

    public partial class frmCaseProperties : Form {
        CaseAction m_action;
        CaseRow cRow;
        CaseDataAccess cdAccess;

        public frmCaseProperties ( ref CaseDataAccess caseDataAccess, CaseAction action ) {
            InitializeComponent();
            cdAccess = caseDataAccess;
            populateLists();
            m_action = action;
            cRow = new CaseRow();
            dtRecievedDate.Value = DateTime.Now;
            dtRecievedTime.Value = DateTime.Now;
        }

        public frmCaseProperties (CaseData.CaseRow caseRow, ref CaseDataAccess caseDataAccess ,CaseAction action) {
            InitializeComponent();
            cRow = caseRow;
            cdAccess = caseDataAccess;
            populateLists();
            m_action = action;
            PopulateForm();
        }

        public void populateLists ( ) {
            cmbStatus.DisplayMember = "Name";
            cmbStatus.ValueMember = "CaseStatus";
            cmbStatus.DataSource = cdAccess.CaseStatusList;


            cmbWhereFrom.DisplayMember = "Name";
            cmbWhereFrom.ValueMember = "WhereFrom";
            cmbWhereFrom.DataSource = cdAccess.WhereFromList;
        }

        public void PopulateForm ( ) {
            //set text values
            txtComplaintId.Text = cRow.ComplaintId.ToString();
            txtCustomerFirstName.Text = cRow.CustomerName;
            txtDescription.Text = cRow.Description.Replace("\n", Environment.NewLine);
            chkNoContactLetter.Checked = cRow.NoContactLetter;
            txtTicketId.Text = cRow.TicketId.ToString();
            //set combo-boxes
            cmbWhereFrom.SelectedValue = cRow.WhereFromId;
            cmbStatus.SelectedValue = cRow.CaseStatusId;
            //set date and time
            dtRecievedDate.Value = cRow.RecievedDateTime;
            dtRecievedTime.Value = cRow.RecievedDateTime;
            //set checkboxes
            chkDayOneCallOne.Checked = cRow.DayOneCallOne;
            chkDayOneEmail.Checked = cRow.DayOneEmail;
            chkDayOneCallTwo.Checked = cRow.DayOneCallTwo;
            chkDayOneCallThree.Checked = cRow.DayOneCallThree;
            chkDayTwoCallOne.Checked = cRow.DayTwoCallOne;
            chkDayTwoCallTwo.Checked = cRow.DayTwoCallTwo;
            chkDayThreeCallOne.Checked = cRow.DayThreeCallOne;
            chkDayFourCallOne.Checked = cRow.DayFourCallOne;
            chkDayFiveCallOne.Checked = cRow.DayFiveCallOne;
            chkDaySixCallOne.Checked = cRow.DaySixCallOne;
            chkDaySevenCallOne.Checked = cRow.DaySevenCallOne;
            chkDayOneCallThree.Checked = cRow.DayOneCallThree;

            //txtNotes.Text = cRow.Notes.Replace("\n", Environment.NewLine); ;
            //txtToDo.Text = cRow.ToDo.Replace("\n", Environment.NewLine); ;
            txtCustomerAdress.Text = cRow.CustomerAdress.Replace("\n", Environment.NewLine); ;
        }

        //private bool ValidateForm ( ) { //not currently used...
        //    long result;
        //    long resultTwo;
        //    return ( Int64.TryParse(txtComplaintId.Text, out resultTwo) && Int64.TryParse(txtTicketId.Text, out result) );
        //}

        private void btnSave_Click(object sender, EventArgs e) {
            cRow.ComplaintId = txtComplaintId.Text;
            cRow.CustomerName = txtCustomerFirstName.Text;
            cRow.Description = txtDescription.Text;
            cRow.NoContactLetter = chkNoContactLetter.Checked;
            cRow.TicketId = txtTicketId.Text;
            cRow.WhereFromId = (WhereFrom) cmbWhereFrom.SelectedValue;
            //TODO: if status before != Closed && cmbStatus ==Closed set CloseDate = DateTime.Now
            cRow.CaseStatusId = (CaseStatus) cmbStatus.SelectedValue;

            cRow.RecievedDateTime = new DateTime(dtRecievedDate.Value.Year, dtRecievedDate.Value.Month, dtRecievedDate.Value.Day, dtRecievedTime.Value.Hour, dtRecievedTime.Value.Minute, 0);
            //set checkboxes
            cRow.DayOneCallOne = chkDayOneCallOne.Checked;
            cRow.DayOneEmail = chkDayOneEmail.Checked;
            cRow.DayOneCallTwo = chkDayOneCallTwo.Checked;
            cRow.DayOneCallThree = chkDayOneCallThree.Checked;
            cRow.DayTwoCallOne = chkDayTwoCallOne.Checked;
            cRow.DayTwoCallTwo = chkDayTwoCallTwo.Checked;
            cRow.DayThreeCallOne = chkDayThreeCallOne.Checked;
            cRow.DayFourCallOne = chkDayFourCallOne.Checked;
            cRow.DayFiveCallOne = chkDayFiveCallOne.Checked;
            cRow.DaySixCallOne = chkDaySixCallOne.Checked;
            cRow.DaySevenCallOne = chkDaySevenCallOne.Checked;
            //cRow.Notes = txtNotes.Text;
            //cRow.ToDo = txtToDo.Text;
            cRow.CustomerAdress = txtCustomerAdress.Text;
            if( m_action == CaseAction.Update ) {
                cdAccess.UpdateCaseRow(cRow);
            }
            else if (m_action == CaseAction.New){
                cdAccess.AddCaseRow(cRow);
            }
            //Enable Save in owner Form
            ((CaseManager) this.Owner).PromptSave = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

    }
}